/*
 * user_register_cb.c
 *
 *  Created on: 2013-2-5
 *      Author: panqiuhui
 */
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <regex.h>
#include "../head/common_struct/userinfo.h"
#include "../head/gui_callback/user_register_cb.h"
#include "../head/gui_callback/global_cb.h"
#include "../head/communicate_with_server/userinfo_transfer.h"

extern GtkBuilder *builder;

//处理工具栏中的用户注册的点击事件
void toolbutton_user_register_clicked_cb(GtkToolButton *toolbutton,
		gpointer user_data) {
	GtkEntry *entry_register_username;
	GtkEntry *entry_register_password;
	GtkEntry *entry_reigster_password_again;
	GtkEntry *entry_register_detail_name;
	GtkRadioButton *radiobutton_register_detail_gender_male;
	GtkRadioButton *radiobutton_register_detail_gender_female;
	GtkEntry *entry_register_birthday;
	GtkCalendar *calendar_register_birthday;
	GtkComboBox *combobox_register_state;
	GtkComboBox *combobox_register_certificate_type;
	GtkEntry *entry_register_detail_certificate_id;
	GtkEntry *entry_register_contact_mobile_phone;
	GtkEntry *entry_register_contact_fixed_telephone;
	GtkEntry *entry_register_contact_email;
	GtkEntry *entry_register_contact_address;
	GtkEntry *entry_register_contact_postcode;
	GtkButton *button_login_noregister;

	entry_register_username =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_register_username"));
	entry_register_password =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_register_password"));
	entry_reigster_password_again =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_reigster_password_again"));
	entry_register_detail_name =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_register_detail_name"));
	radiobutton_register_detail_gender_male =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_register_detail_gender_male"));
	radiobutton_register_detail_gender_female =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_register_detail_gender_female"));
	entry_register_birthday =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_register_birthday"));
	calendar_register_birthday =
			GTK_CALENDAR(gtk_builder_get_object(builder,"calendar_register_birthday"));
	combobox_register_state = GTK_COMBO_BOX(
			gtk_builder_get_object(builder, "combobox_register_state"));
	combobox_register_certificate_type = GTK_COMBO_BOX(
			gtk_builder_get_object(builder,
					"combobox_register_certificate_type"));
	entry_register_detail_certificate_id =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_register_detail_certificate_id"));
	entry_register_contact_mobile_phone =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_register_contact_mobile_phone"));
	entry_register_contact_fixed_telephone =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_register_contact_fixed_telephone"));
	entry_register_contact_email =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_register_contact_email"));
	entry_register_contact_address =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_register_contact_address"));
	entry_register_contact_postcode =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_register_contact_postcode"));
	button_login_noregister =
			GTK_BUTTON(gtk_builder_get_object(builder,"button_login_noregister"));

	gtk_entry_set_text(entry_register_username, "");
	gtk_entry_set_text(entry_register_password, "");
	gtk_entry_set_text(entry_reigster_password_again, "");
	gtk_entry_set_text(entry_register_detail_name, "");
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
			radiobutton_register_detail_gender_male), true);
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(
			radiobutton_register_detail_gender_female), false);

	struct tm *tm_ptr;
	time_t the_time;
	(void) time(&the_time);
	tm_ptr = localtime(&the_time);
	char birthday[BIRTHDAY_LEN];
	memset(birthday, 0, BIRTHDAY_LEN);
	sprintf(birthday, "%04d-%02d-%02d", tm_ptr->tm_year + 1900,
			tm_ptr->tm_mon + 1, tm_ptr->tm_mday);
	gtk_entry_set_text(entry_register_birthday, birthday);
	gtk_calendar_select_month(calendar_register_birthday, tm_ptr->tm_mon,
			tm_ptr->tm_year + 1900);
	gtk_calendar_select_day(calendar_register_birthday, tm_ptr->tm_mday);

	gtk_combo_box_set_active(combobox_register_state, 0);
	gtk_combo_box_set_active(combobox_register_certificate_type, 0);
	gtk_entry_set_text(entry_register_detail_certificate_id, "");
	gtk_entry_set_text(entry_register_contact_mobile_phone, "");
	gtk_entry_set_text(entry_register_contact_fixed_telephone, "");
	gtk_entry_set_text(entry_register_contact_email, "");
	gtk_entry_set_text(entry_register_contact_address, "");
	gtk_entry_set_text(entry_register_contact_postcode, "");
	if (check_is_login()) {
		gtk_widget_hide(GTK_WIDGET(button_login_noregister) );
	} else {
		gtk_widget_show(GTK_WIDGET(button_login_noregister) );
	}

	window_show(REGISTER);
}

//处理用户注册的提交按钮的点击事件
void button_register_clicked_cb(GtkButton *button, gpointer user_data) {
	GtkEntry *entry_register_username;
	GtkEntry *entry_register_password;
	GtkEntry *entry_reigster_password_again;
	GtkEntry *entry_register_detail_name;
	GtkRadioButton *radiobutton_register_detail_gender_male;
	GtkEntry *entry_register_birthday;
	GtkComboBox *combobox_register_state;
	GtkComboBox *combobox_register_certificate_type;
	GtkEntry *entry_register_detail_certificate_id;
	GtkEntry *entry_register_contact_mobile_phone;
	GtkEntry *entry_register_contact_fixed_telephone;
	GtkEntry *entry_register_contact_email;
	GtkEntry *entry_register_contact_address;
	GtkEntry *entry_register_contact_postcode;
	GtkWindow *global_window;

	global_window =
			GTK_WINDOW(gtk_builder_get_object(builder, "global_window"));
	entry_register_username =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_register_username"));
	entry_register_password =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_register_password"));
	entry_reigster_password_again =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_reigster_password_again"));
	entry_register_detail_name =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_register_detail_name"));
	radiobutton_register_detail_gender_male =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_register_detail_gender_male"));
	entry_register_birthday =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_register_birthday"));
	combobox_register_state = GTK_COMBO_BOX(
			gtk_builder_get_object(builder, "combobox_register_state"));
	combobox_register_certificate_type = GTK_COMBO_BOX(
			gtk_builder_get_object(builder,
					"combobox_register_certificate_type"));
	entry_register_detail_certificate_id =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_register_detail_certificate_id"));
	entry_register_contact_mobile_phone =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_register_contact_mobile_phone"));
	entry_register_contact_fixed_telephone =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_register_contact_fixed_telephone"));
	entry_register_contact_email =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_register_contact_email"));
	entry_register_contact_address =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_register_contact_address"));
	entry_register_contact_postcode =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_register_contact_postcode"));

	const gchar *username = gtk_entry_get_text(entry_register_username);
	int username_length = strlen(username);
	if (username_length == 0) {
		gtk_show_warning(global_window, "%s", "用户名不能为空", "输入提醒");
		return;
	}
	char *pattern = "^[0-9|a-z|A-Z|_]{6,30}$";
	bool check_result = do_regex(username, pattern);
	if (!check_result) {
		gtk_show_warning(global_window, "%s", "用户名不符合要求，请重新输入", "输入提醒");
		return;
	}

	const gchar *password = gtk_entry_get_text(entry_register_password);
	int password_length = strlen(password);
	if (password_length < 6) {
		gtk_show_warning(global_window, "%s", "密码不能少于6位字符", "输入提醒");
		return;
	}

	const gchar *pw_again = gtk_entry_get_text(entry_reigster_password_again);
	if (strcmp(password, pw_again) != 0) {
		gtk_show_warning(global_window, "%s", "两次输入的密码不一致", "输入提醒");
		return;
	}

	const gchar *name = gtk_entry_get_text(entry_register_detail_name);
	int name_length = g_utf8_strlen(name, NAME_LEN);
	if (name_length == 0) {
		gtk_show_warning(global_window, "%s", "姓名不能为空", "输入提醒");
		return;
	}

	gboolean ismale = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
			radiobutton_register_detail_gender_male) );

	const gchar *birthday = gtk_entry_get_text(entry_register_birthday);

	const gchar *country;
	GtkTreeModel *model_country = gtk_combo_box_get_model(
			combobox_register_state);
	GtkTreeIter iter_country;
	gtk_combo_box_get_active_iter(combobox_register_state, &iter_country);
	gtk_tree_model_get(model_country, &iter_country, 0, &country, -1);

	const gchar *credit_type;
	GtkTreeModel *model_credit_type = gtk_combo_box_get_model(
			combobox_register_certificate_type);
	GtkTreeIter iter_credit_type;
	gtk_combo_box_get_active_iter(combobox_register_certificate_type,
			&iter_credit_type);
	gtk_tree_model_get(model_credit_type, &iter_credit_type, 0, &credit_type,
			-1);

	const gchar *creditID = gtk_entry_get_text(
			entry_register_detail_certificate_id);
	int creditID_length = g_utf8_strlen(creditID, CREDIT_ID_LEN);
	if (creditID_length == 0) { //检查证件号码是否为空
		gtk_show_warning(global_window, "%s", "证件号码不能为空", "输入提醒");
		return;
	}

	const gchar *mobilephone_num = gtk_entry_get_text(
			entry_register_contact_mobile_phone);
	int mobilephone_num_length = g_utf8_strlen(mobilephone_num,
			MOBILE_PHONE_NUM_LEN);
	if (mobilephone_num_length == 0) { //检查手机号码是否为空
		gtk_show_warning(global_window, "%s", "手机号码不能为空", "输入提醒");
		return;
	}

	const gchar *fixedphone_num = gtk_entry_get_text(
			entry_register_contact_fixed_telephone);

	const gchar *email = gtk_entry_get_text(entry_register_contact_email);
	int email_length = g_utf8_strlen(email, EMAIL_LEN);
	if (email_length == 0) { //检查电子邮件是否为空
		gtk_show_warning(global_window, "%s", "电子邮件不能为空", "输入提醒");
		return;
	}

	const gchar *address = gtk_entry_get_text(entry_register_contact_address);
	const gchar *postcode = gtk_entry_get_text(entry_register_contact_postcode);

	struct userinfo user;
	memset(&user, 0, sizeof(user));
	strncpy(user.username, username, username_length);

	strncpy(user.password, password, password_length);

	g_utf8_strncpy(user.name, name, name_length);

	if (ismale) {
		int male_length = g_utf8_strlen("男", GENDER_LEN);
		g_utf8_strncpy(user.gender, "男", male_length);
	} else {
		int female_length = g_utf8_strlen("女", GENDER_LEN);
		g_utf8_strncpy(user.gender, "女", female_length);
	}

	strncpy(user.birthday, birthday, BIRTHDAY_LEN);

	int country_length = g_utf8_strlen(country, COUNTRY_LEN);
	g_utf8_strncpy(user.country, country, country_length);

	int credit_type_length = g_utf8_strlen(credit_type, CREDIT_TYPE_LEN);
	g_utf8_strncpy(user.creditType, credit_type, credit_type_length);

	g_utf8_strncpy(user.creditID, creditID, creditID_length);

	g_utf8_strncpy(user.mobilephoneNum, mobilephone_num,
			mobilephone_num_length);

	int fixedphone_num_length = g_utf8_strlen(fixedphone_num,
			FIXED_PHONE_NUM_LEN);
	g_utf8_strncpy(user.fixedphoneNum, fixedphone_num, fixedphone_num_length);

	g_utf8_strncpy(user.email, email, email_length);

	int address_length = g_utf8_strlen(address, ADDRESS_LEN);
	g_utf8_strncpy(user.address, address, address_length);

	int postcode_length = g_utf8_strlen(postcode, POSTCODE_LEN);
	g_utf8_strncpy(user.postcode, postcode, postcode_length);

	char *type = "customer";
	strncpy(user.type, type, strlen(type));

	struct userinfo_request request;
	memset(&request, 0, sizeof(request));
	request.user = user;
	bool result = register_new_user(&request);
	if (result) {
		gtk_show_info(global_window, "%s", "注册成功", "信息提示");
		toolbutton_user_register_clicked_cb(NULL, NULL );
	} else {
		gtk_show_warning(global_window, "%s", "注册失败，该用户名已存在，请重新填写", "信息提示");
	}
}

//正则表达式验证
bool do_regex(const char *src, const char *pattern) {
	bool result = true;
	regex_t re;
	int err;

	err = regcomp(&re, pattern, REG_EXTENDED);
	if (err) {
		int errlen = (int) regerror(err, &re, NULL, 0);
		char errbuf[errlen];
		regerror(err, &re, errbuf, errlen);
		printf("REGEX ERROR regcomp: %s\n", errbuf);
		result = false;
		exit(-1);
	} else {
		int nsub = (int) re.re_nsub;
		int nsubs = nsub + 1;
		regmatch_t subs[nsubs];
		err = regexec(&re, src, nsubs, subs, 0);
		if (err == REG_NOMATCH) {
			//printf("Sorry,no match...\n");
			result = false;
		} else if (err) {
			int errlen = (int) regerror(err, &re, NULL, 0);
			char errbuf[errlen];
			regerror(err, &re, errbuf, errlen);
			printf("REGEX ERROR regexec: %s\n", errbuf);
			result = false;
			exit(-1);
		}
	}
	regfree(&re);
	return result;
}

//处理用户注册后登录的按钮的点击事件
void button_login_noregister_clicked_cb(GtkButton *button, gpointer user_data) {
	GtkEntry *entry_login_username;
	GtkEntry *entry_login_password;
	GtkLabel *label_login_warn;

	entry_login_username =
			GTK_ENTRY(gtk_builder_get_object(builder, "entry_login_username"));
	entry_login_password =
			GTK_ENTRY(gtk_builder_get_object(builder, "entry_login_password"));
	label_login_warn =
			GTK_LABEL(gtk_builder_get_object(builder, "label_login_warn"));

	gtk_entry_set_text(entry_login_username, ""); //清空用户名输入框
	gtk_entry_set_text(entry_login_password, ""); //清空密码输入框
	gtk_label_set_label(label_login_warn, ""); //清空提示label

	window_show(LOGIN);
}
